using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using XNAPlatformer.Source.Weapons;

namespace XNAPlatformer.Source.Characters
{
    public class Enemy
    {
         private Texture2D texture;
        private Weapon weapon;
        private Vector2 position;
        private float velocity;
        private float downVelocity;
        private bool isJumping;
        private bool jumpEnabled;
        private int jumpCounter;
        private int feetHeight;
        private int topHeight;
        private Vector2 direction;
        private Color[] textureColTop;
        private Color[] textureColBottom;
        private Color[] textureWhole;
        private bool isDead;

        #region Accessors
        public bool IsDead
        {
            get
            {
                return isDead;
            }
            set
            {
                isDead = value;
            }
        }


        public Color[] Colors
        {
            get
            {
                return textureWhole;
            }
        }
        #endregion

        public Enemy(ContentManager content, Vector2 position, Vector2 direction)
        {
            this.texture = content.Load<Texture2D>("Content\\Textures\\Characters\\tempPic");
            this.position = position;
            velocity = 3.0f;
            downVelocity = 4.0f;
            jumpCounter = 0;
            isJumping = false;
            feetHeight = 55;
            topHeight = texture.Height - feetHeight;
            weapon = new Weapon(content);
            this.direction = direction;
            textureColTop = new Color[(texture.Height - topHeight) * texture.Width];
            textureColBottom = new Color[(texture.Height - feetHeight)];
            textureWhole = new Color[texture.Width * texture.Height];
            texture.GetData(0, new Rectangle(0, 0, texture.Width, texture.Height - topHeight), textureColTop, 0, (texture.Height - topHeight) * texture.Width);
            texture.GetData(0, new Rectangle((int)(texture.Width / 2.0f), feetHeight, 1, texture.Height - feetHeight), textureColBottom, 0, (texture.Height - feetHeight));
            texture.GetData(textureWhole);
            isDead = false;

        }

        public void Update(GameTime gameTime)
        {
            

            position += direction * velocity;


            #region Movement Checks
            if (XNAPlatformer.IntersectPixels(new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height - topHeight), textureColTop,
                new Rectangle(GetFocus(), 0, 1280, 480), GetAlpha()) == true)
            {
                position -= direction * velocity;
            }

            if (position.X > 640 - texture.Width)
            {
                position.X = 640 - texture.Width;
            }

            #endregion

            #region Jumping logic

            /*

            if (gamePad.Buttons.A == ButtonState.Pressed && aIsPressed == false && jumpEnabled == true)
            {
                aIsPressed = true;
                isJumping = true;

            }
            else if (gamePad.Buttons.A == ButtonState.Released)
            {
                aIsPressed = false;
            }
            
            if (isJumping == true)
            {
                jumpCounter++;
                position.Y -= downVelocity * 2;

                if (jumpCounter > 25)
                {
                    jumpCounter = 0;
                    isJumping = false;
                }
            }
             */
            #endregion

            #region Shooting
            /*
            if (gamePad.Buttons.X == ButtonState.Pressed && xIsPressed == false)
            {
                xIsPressed = true;
                Vector2 direction = gamePad.ThumbSticks.Left;

                //check if direction is (0,0), if so, change to 1,0 or -1,0 depending on direction, also, when prone is in place, only shoot in x direction)
                if (direction == Vector2.Zero)
                {
                    direction = new Vector2(1, 0);
                }
                
                XNAPlatformer.activeBullets.Add(new Bullet(position + new Vector2(70,20), direction, weapon.BulletTexture, weapon.BulletVelocity));
            }
            else if (gamePad.Buttons.X == ButtonState.Released)
            {
                xIsPressed = false;
            }
            */
            #endregion

            #region Floor Collision
            GetHeight();
            #endregion

            #region Check if colliding with bullets
            foreach (Bullet bullet in XNAPlatformer.activeBullets)
            {
                if (XNAPlatformer.IntersectPixels(bullet.BoundingRectangle(), bullet.Colors, new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height), textureWhole) == true)
                {
                    isDead = true;
                    bullet.IsDead = true;
                }
            }
            #endregion


        }

        public void Draw(GameTime gameTime, SpriteBatch spritebatch)
        {
            spritebatch.Draw(texture, position, Color.White);
        }

        public void GetHeight()
        {
          GoDown();
        //GoUp();
        }

        public void GoDown()
        {
            if (XNAPlatformer.IntersectPixels(new Rectangle((int)position.X + (int)(texture.Width / 2.0f), (int)position.Y + feetHeight, 1, texture.Height - feetHeight), textureColBottom,
                new Rectangle(GetFocus(), 0, 1280, 480), GetAlpha()) == false)
            {
                position.Y += downVelocity;
                jumpEnabled = false;
            }
            else
            {
                jumpEnabled = true;
                GoUp();
            }


        }

        public void GoUp()
        {
            if (XNAPlatformer.IntersectPixels(new Rectangle((int)position.X + (int)(texture.Width / 2.0f), (int)position.Y + feetHeight, 1, texture.Height - feetHeight), textureColBottom,
                new Rectangle(GetFocus(), 0, 1280, 480), GetAlpha()) == true && XNAPlatformer.IntersectPixels(new Rectangle((int)position.X + (int)(texture.Width / 2.0f), (int)position.Y + feetHeight - (int)downVelocity, 1, texture.Height - feetHeight), textureColBottom,
                new Rectangle(GetFocus(), 0, 1280, 480), GetAlpha()) == true && XNAPlatformer.IntersectPixels(new Rectangle((int)position.X + (int)(texture.Width / 2.0f), (int)position.Y + feetHeight - (int)downVelocity * 5, 1, texture.Height - feetHeight), textureColBottom,
                new Rectangle(GetFocus(), 0, 1280, 480), GetAlpha()) == false)
            {
                position.Y -= downVelocity;
            }
        }

        public Color[] GetAlpha()
        {
            return XNAPlatformer.currentLevel.alphaMapColors;
        }

        public int GetFocus()
        {
            return XNAPlatformer.currentLevel.GetCurrentFocus();
        }

        public Rectangle BoundingRectangle()
        {
            return (new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height));
        }
       
    }
}

